*------------------------------------------------------------------------------*
*																			   *
*							TV-Content 2013								   	   *
*																			   *
*------------------------------------------------------------------------------*

* Project: "Voting in Context" Book
* Title: "Always late? Stability and change in individuals‘ time of vote decisions"
* Author: Maria Preißinger


use ZA5705_v1-0-0.dta, clear		
		
drop if filter_bei==0		//at the article-level
drop if B02==2				//topically not classifiable: greetings opening credits etc.


tab B02 B06, m	// 100% of topically not classifiable (B05=2) are missings in B10


fre B06		

fre B10
tab B06 B10, m

*---------------------------------------
* Date
*---------------------------------------

gen date = mdy(V04, V05, year)  //in Stata-Date format
format date %d  //make it readable				
					
					
*-------------------------------------
* evaluation of articles
*------------------------------------------

recode B20a-B20k (0=.) (1=1) (2=0.5) (3 9=0) (4=-0.5) (5=-1) (.=.)
lab val B20a-B20k


 *CDU
egen toncdurtl = rowmean(B20a B20b B20c) if  filter_dpol==1 &  V02=="RTL aktuell" 
egen toncdusat = rowmean(B20a B20b B20c) if  filter_dpol==1 &  V02=="Sat.1 Nachrichten" 
egen toncduard = rowmean(B20a B20b B20c) if  filter_dpol==1 &  V02=="Tagesschau" 
egen toncduzdf = rowmean(B20a B20b B20c) if  filter_dpol==1 &  V02=="heute" 

*SPD
egen tonspdrtl = rowmean(B20d B20e) if  filter_dpol==1 &  V02=="RTL aktuell" 
egen tonspdsat = rowmean(B20d B20e) if  filter_dpol==1 &  V02=="Sat.1 Nachrichten" 
egen tonspdard = rowmean(B20d B20e) if  filter_dpol==1 &  V02=="Tagesschau" 
egen tonspdzdf = rowmean(B20d B20e) if  filter_dpol==1 &  V02=="heute"

*FDP
egen tonfdprtl = rowmean(B20f B20g) if  filter_dpol==1 &  V02=="RTL aktuell"
egen tonfdpsat = rowmean(B20f B20g) if  filter_dpol==1 &  V02=="Sat.1 Nachrichten"
egen tonfdpard = rowmean(B20f B20g) if  filter_dpol==1 &  V02=="Tagesschau"
egen tonfdpzdf = rowmean(B20f B20g) if  filter_dpol==1 &  V02=="heute"

	
*Greens
egen tongrurtl = rowmean(B20h B20i) if  filter_dpol==1 &  V02=="RTL aktuell"
egen tongrusat = rowmean(B20h B20i) if  filter_dpol==1 &  V02=="Sat.1 Nachrichten"
egen tongruard = rowmean(B20h B20i) if  filter_dpol==1 &  V02=="Tagesschau"
egen tongruzdf = rowmean(B20h B20i) if  filter_dpol==1 &  V02=="heute"

*The Left
egen tonlinkrtl = rowmean(B20j B20k) if  filter_dpol==1 &  V02=="RTL aktuell"
egen tonlinksat = rowmean(B20j B20k) if  filter_dpol==1 &  V02=="Sat.1 Nachrichten"
egen tonlinkard = rowmean(B20j B20k) if  filter_dpol==1 &  V02=="Tagesschau"
egen tonlinkzdf = rowmean(B20j B20k) if  filter_dpol==1 &  V02=="heute"



*----------------------------------------
* aggregate for each day
*----------------------------------------


collapse (mean) ton*, by(date)

expand 2 if  date==date("21sep2013", "DMY"), gen(expanded)

/* survey of the TV-News ends before the campaing panel does. As we want to assign the 
average tone of conversation of a week before the the interview, we can add one day.
Solution: 
Copy the last observation and append it below. That works with the "expand"-command.
Set values of all variables for the entire additional day to "missing". */	


gen n = _n			//create serial number
order n				//right in the beginning of the dataset
					
replace date = date+1 if  n==92		//adjust date for these observations

foreach var of varlist toncdurtl-tonlinkzdf {
replace `var' = . if  n>=92
}


*-----------------------------------------------
* create lags for up to 7 days before
*------------------------------------------------


foreach s in ard zdf rtl sat {
foreach num of numlist 1/7 {
foreach partei in cdu spd fdp gru link {
	gen lg`num'ton`partei'`s' = ton`partei'`s'[_n-`num']  
}
}  
}


*-------------------------------------------
* Statement Bias
*-------------------------------------------

foreach s in ard zdf rtl sat {
foreach partei in cdu spd fdp gru link {
egen finalton`partei'`s' = rowtotal(lg?ton`partei'`s'), missing
replace finalton`partei'`s' = finalton`partei'`s'/7
replace finalton`partei'`s' = . if    date<date("30jun2013", "DMY")		
	//delete first seven days 
}
}

list date toncduard lg?toncduard finaltoncduard	in 1/10	//now we can merge it

fre finalton*
sum finalton*		//number of observations is correct: 98 minus 7 days


*-------------------------------------------
* prepare for merging-process
*-----------------------------------------------

keep date finalton* 


/* Create duplicate for each wave: Corresponding observations will be assigned 
with the "merge"-command. Also: add the prefix "b_" to all variables */

		
foreach var of varlist finalton*  {
foreach num of numlist 1/7 {
gen b_`var'`num' = `var'
}
}

foreach num of numlist 1/7 {
clonevar b_date`num' = date
}

keep b_*

save tvcontent2013.dta, replace


